package com.findtech.threePomelos.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.utils.RequestUtils;

import java.util.ArrayList;

/**
 * Created by zhi.zhang on 3/19/16.
 */
public class AboutAdapter extends RecyclerView.Adapter<AboutAdapter.MyViewHolder> {

    RequestUtils.MyItemClickListener mItemClickListener;
    Context mContext;
    ArrayList<String> mListItem = new ArrayList<>();
    String mFirmwareVersion;
    String mCurrentElectric;

    public AboutAdapter(Context context) {
        mContext = context;
    }

    @Override
    public AboutAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AboutAdapter.MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).
                inflate(R.layout.app_about_list_item, parent, false));//不添加false，会出现crash
        return holder;
    }

    @Override
    public void onBindViewHolder(AboutAdapter.MyViewHolder holder, int position) {
        holder.appInfoTitle.setText(mListItem.get(position));
        String firmwareVersion = mContext.getResources().
                getString(R.string.app_device_text_check_firmware_version);
        String currentElectric= mContext.getResources().
                getString(R.string.app_device_text_current_electric);
        if (!"".equals(mFirmwareVersion) && firmwareVersion.equals(mListItem.get(position))) {
            holder.arrowRight.setVisibility(View.GONE);
            holder.firmwareVersion.setVisibility(View.VISIBLE);
            holder.firmwareVersion.setText(mFirmwareVersion);
        }
        if (!"".equals(mCurrentElectric) && currentElectric.equals(mListItem.get(position))) {
            holder.arrowRight.setVisibility(View.GONE);
            holder.firmwareVersion.setVisibility(View.VISIBLE);
            holder.firmwareVersion.setText(mCurrentElectric);
        }
    }

    @Override
    public int getItemCount() {
        return mListItem.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView appInfoTitle;
        ImageView arrowRight;
        TextView firmwareVersion;

        public MyViewHolder(View itemView) {
            super(itemView);
            appInfoTitle = (TextView) itemView.findViewById(R.id.app_info_title);
            firmwareVersion = (TextView) itemView.findViewById(R.id.app_info_firmware_version);
            arrowRight = (ImageView) itemView.findViewById(R.id.app_info_arrow_right);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(view, getAdapterPosition());
                    }
                }
            });
        }
    }

    public void setOnItemClickListener(RequestUtils.MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setListItem(ArrayList<String> listItem) {
        mListItem = listItem;
    }

    public void setFirmwareVersion(String firmwareVersion) {
        mFirmwareVersion = firmwareVersion;
    }

    public void setCurrentElectric(String currentElectric) {
        mCurrentElectric = currentElectric;
    }
}
